﻿using WpfMvvmTemplatePrismHC.Common;
using WpfMvvmTemplatePrismHC.Models.BindingModels;
using WpfMvvmTemplatePrismHC.Tools.Helpers;

namespace WpfMvvmTemplatePrismHC.ViewModels.Main
{
    public class MainWindowViewModel : BindableBase
    {
        /// <summary>
        /// MainWindow 模型
        /// </summary>
        public MainWindowModel? MainWindowModels { get; set; }

        //区域管理器
        private readonly IRegionManager? regionManager;

        //导航日志
        private IRegionNavigationJournal? journal;

        public DelegateCommand<string>? GoBackCommand { get; private set; }
        public DelegateCommand<string>? GoForwardCommand { get; private set; }
        public DelegateCommand<string>? NavigateCommand { get; private set; }

        public MainWindowViewModel()
        {
            if (GlobalStaticData.IsDesignTime)
            {
                MainWindowModels = new();
            }

        }

        public MainWindowViewModel(IRegionManager regionManager)
        {
            MainWindowModels = new();
            this.regionManager = regionManager;

            //从App类中获取区域管理器
            var regionManager2 = (Application.Current as App)?.Container.Resolve<IRegionManager>();

            GoBackCommand = new DelegateCommand<string>(GoBack);
            GoForwardCommand = new DelegateCommand<string>(GoForward);
            NavigateCommand = new DelegateCommand<string>(Navigate);
        }

        /// <summary>
        /// 上一步
        /// </summary>
        /// <param name="obj"></param>
        private void GoBack(string obj)
        {
            if (journal is not null && journal.CanGoBack)
                journal.GoBack();
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <param name="obj"></param>
        private void GoForward(string obj)
        {
            if (journal is not null && journal.CanGoForward)
                journal.GoForward();
        }

        /// <summary>
        /// 导航
        /// </summary>
        /// <param name="viewName">页面名称</param>
        private void Navigate(string viewName)
        {
            if (viewName is null || string.IsNullOrWhiteSpace(viewName)) return;

            //传递参数字典
            var param = new NavigationParameters
            {
                { "Message", viewName }
            };

            //param.Add("Hello","World");

            //从App类中获取区域管理器
            var regionManager2 = (Application.Current as App)?.Container.Resolve<IRegionManager>();

            //导航到指定视图 两种方式
            regionManager?.Regions["ContentRegion"].RequestNavigate(viewName, back =>
            {
                journal = back.Context.NavigationService.Journal;
            }, param);

            //regionManager?.RequestNavigate("ContentRegion", viewName, back =>
            //{
            //    journal = back.Context.NavigationService.Journal;
            //}, param);

        }
    }
}